using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

using DevExpress.XtraTreeList.Nodes;


namespace ERP.View
{
    public partial class viewListDonvi : DevExpress.XtraEditors.XtraForm
    {
        public viewListDonvi()
        {
            InitializeComponent();
        }

        public cs_hmr_nhanvienInfo nv = new cs_hmr_nhanvienInfo();
        //public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
        public cs_hmr_donviInfo donvi = new cs_hmr_donviInfo();
        public int flag = 0;

        private void viewListDonvi_Load(object sender, EventArgs e)
        {
            this.Load_Donvi();
            this.Load_CBO();
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    this.btnXoa.Enabled = false;
                }
                if (userright.updatedata == 0)
                {
                    this.btnSua.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }

        private void Load_Donvi()
        {
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            this.trlDonvi.DataSource = dv.Get_List().Tables[0];
            this.trlDonvi.KeyFieldName = "madonvi";
            this.trlDonvi.ParentFieldName = "captren";
            this.trlDonvi.ExpandAll();
        }

        private void Load_CBO()
        {
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            DataSet ds = new DataSet("ds");
            ds = dv.Get_List_1();
            this.cboCaptren.Properties.DataSource = ds.Tables[0];
            cs_hmr_loaidonviInfo loaidv = new cs_hmr_loaidonviInfo();
            ds = new DataSet("ds");
            ds = loaidv.Get_List();
            this.cboLoaiDV.Properties.DataSource = ds.Tables[0];
        }

        

        private void btnMoi_Click(object sender, EventArgs e)
        {
            this.flag = 1;
            this.btnSua.Enabled = false;
            this.btnLuu.Enabled = true;
            this.btnMoi.Enabled = false;
            this.ClearText();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

        private void ClearText()
        {
            this.txtMaDV.Text = "";
            this.txtTenDV.Text = "";
            this.txtDiachi.Text = "";
            this.txtChucnang.Text = "";
            this.txtMasothue.Text = "";
            this.txtDienthoai.Text = "";
            
            this.txtSoQD.Text = "";
            this.txtNoidung.Text = "";
        }
        private void Load_Info()
        {
            this.txtMaDV.Text = this.donvi.madonvi;
            this.txtTenDV.Text = this.donvi.tendonvi;
            this.txtDiachi.Text = this.donvi.diachi;
            this.txtChucnang.Text = this.donvi.chucnang;
            this.txtMasothue.Text = this.donvi.msthue;
            this.txtDienthoai.Text = this.donvi.sodt_fax;
            this.txtNgaythanhlap.Text = this.donvi.ngaythanhlap;
            this.txtSoQD.Text = this.donvi.qdthanhlap;
            this.txtNoidung.Text = this.donvi.noidungthanhlap;
            this.cboLoaiDV.EditValue = this.donvi.loaidv;//.ToString().Trim();
            this.cboCaptren.EditValue = this.donvi.captren;
        }

        private void Save_Info()
        {
            this.donvi.madonvi = this.txtMaDV.Text.Trim();
            this.donvi.tendonvi = this.txtTenDV.Text.Trim();
            this.donvi.diachi = this.txtDiachi.Text.Trim();
            this.donvi.chucnang = this.txtChucnang.Text.Trim();
            this.donvi.msthue = this.txtMasothue.Text.Trim();
            this.donvi.sodt_fax = this.txtDienthoai.Text.Trim();
            this.donvi.ngaythanhlap = this.txtNgaythanhlap.Text;
            this.donvi.qdthanhlap = this.txtSoQD.Text.Trim();
            this.donvi.noidungthanhlap = this.txtNoidung.Text.Trim();

            this.donvi.loaidv = int.Parse(this.cboLoaiDV.EditValue.ToString().Trim());
            this.donvi.captren = this.cboCaptren.EditValue.ToString().Trim();
            if (this.flag == 1)
            {
                this.donvi.nguoinhap = this.user.id_nv;
                this.donvi.ngaynhap = DateTime.Now.ToShortDateString();
                this.donvi.nguoisua = this.user.id_nv;
                this.donvi.ngaysua = DateTime.Now.ToShortDateString();
            }
            else
            {
                this.donvi.nguoisua = this.user.id_nv;
                this.donvi.ngaysua = DateTime.Now.ToShortDateString();
            }
        }

        private void trlDonvi_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                TreeListNode node = e.Node;
                this.donvi.madonvi = node.GetValue("madonvi").ToString();
                if (this.donvi.Get_Info() > 0)
                {
                    this.flag = 2;
                    this.btnLuu.Enabled = true;
                    this.btnSua.Enabled = false;
                    this.Load_Info();
                }
            }
            catch
            {
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            this.Save_Info();
            if (this.donvi.madonvi == "" || this.donvi.tendonvi == "")
            {
            }
            else
            {
                if(this.donvi.ExcuteProcedure(this.flag) > 0)
                {
                    this.btnLuu.Enabled = false;
                    this.btnMoi.Enabled = true;
                    this.btnSua.Enabled = true;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.flag = 2;
            //this.btnXoa.Enabled = false;
            //this.btnLuu.Enabled = true;
            this.donvi.madonvi = this.trlDonvi.FocusedNode.GetValue(trlcMa).ToString().Trim();
            if (this.donvi.Get_Info() > 0)
            {
                this.donvi.trangthai = 0;
                this.donvi.ngayhuy = DateTime.Now.ToShortDateString();
                this.donvi.ExcuteProcedure(this.flag);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            this.donvi.madonvi = this.trlDonvi.FocusedNode.GetValue(trlcMa).ToString().Trim();
            if (this.donvi.Get_Info() > 0)
            {
                this.flag = 2;
                this.btnLuu.Enabled = true;
                this.btnSua.Enabled = false;
                this.Load_Info();
            }
        }
    }
}